/*
 * @Author: your name
 * @Date: 2022-03-21 16:05:15
 * @LastEditTime: 2022-11-15 11:34:07
 * @LastEditors: YNT 2501512800@qq.com
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: \22-03-19 智慧社区\behaviors\user.js
 */
import Dialog from '@vant/weapp/dialog/dialog'
import globalModel from '../api/global'
const computedBehavior = require('miniprogram-computed').behavior
export default Behavior({
  behaviors: [computedBehavior],
  properties: {},
  data: {
    url: '',
    need_update: true,
    need_phone: true,
  },

  watch: {
    'need_update, need_phone': function (need_update, need_phone) {
      let { url } = this.data
      if (!need_update && !need_phone) {
        wx.redirectTo({ url })
      }
    },
  },
  methods: {
    //#region 上传用户信息 / 手机号
    getUserInfo({ type, detail }) {
      // let isUpdata = wx.getStorageSync('isUpdata') || {
      //   need_update: true,
      //   need_phone: true,
      // }
      let isUpdata = {
        need_update: true,
        need_phone: true,
      }
      switch (type) {
        case 'getphonenumber':
          const { iv, encryptedData } = detail
          const { code } = this.data
          if (iv && encryptedData) {
            globalModel
              .setPhone({
                iv,
                encryptedData,
                code,
              })
              .then(({ data }) => {
                isUpdata.need_phone = false
                this.setData({ need_phone: false })
                setTimeout(() => {
                  wx.setStorageSync('isUpdata', isUpdata)
                }, 300)
              })
          }
          break
        default:
          // 上传用户信息
          wx.getUserProfile({ desc: '用于完善用户资料' }).then((data) => {
            globalModel
              .setUserInfo({
                nick_name: data.userInfo.nickName,
                avatar_url: data.userInfo.avatarUrl,
                sex: data.userInfo.gender,
              })
              .then(({ data }) => {
                isUpdata.need_update = false
                this.setData({ need_update: false })
                setTimeout(() => {
                  wx.setStorageSync('isUpdata', isUpdata)
                  wx.setStorageSync('userInfo', data)
                }, 600)
              })
              .catch((err) => {})
          })
          break
      }
    },

    setAuth(e) {
      let { url } = e.currentTarget.dataset
      this.setData({ url })
      let { need_update = true, need_phone = true } =
        wx.getStorageSync('isUpdata')
      let message = ''
      if (need_update && need_phone) {
        message = '需要获取您微信头像、昵称，和手机号，用于生成通行证'
      }
      if (need_update && !need_phone) {
        message = '个人信息不完整，请授权获取您微信头像、昵称，用于生成通行证'
      }
      if (!need_update && need_phone) {
        message = '个人信息不完整，请授权获取您手机号，用于生成通行证'
      }
      if (message) {
        if (need_phone)
          wx.login().then(({ code }) => {
            this.data.code = code
          })
        Dialog.confirm({
          confirmButtonOpenType: need_phone && 'getPhoneNumber',
          title: '提示',
          message,
        }).catch(() => Dialog.close())
        return true
      } else if (url) {
        wx.redirectTo({ url })
      }
    },
    //#endregion
  },
})
